//
//  ViewController.m
//  CoreData
//
//  Created by honey on 16/4/29.
//  Copyright © 2016年 honey. All rights reserved.
//

#import "ViewController.h"

#import "Person.h"
#import "Cat.h"
#import "AppDelegate.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self select];
    
    NSLog(@"%@", NSHomeDirectory());
}

//查询
- (void)select
{
    //系统的单例
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    
    NSManagedObjectContext *context = delegate.managedObjectContext;
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
    //创建排序依据
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
    
    //设置排序条件
    request.sortDescriptors = @[sort];
  
    NSArray *result = [context executeFetchRequest:request error:nil];
    
    for (Person *p in result)
    {
        NSLog(@"%@", p.age);
    }
    
    /*
     数组排序
    NSSortDescriptor *s = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
    
    NSArray *a = @[@"123", @"1234", @"12345", @"1234567"];
    
    NSArray *r = [a sortedArrayUsingDescriptors:@[s]];
    
    NSLog(@"r = %@", r);
     */

}
//删除
- (void)delete
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = delegate.managedObjectContext;
    
    //1.先找到要删除的内容
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
    NSPredicate *where = [NSPredicate predicateWithFormat:@"age <= %ld", 20];
    
    request.predicate = where;
    
    NSArray *result = [context executeFetchRequest:request error:nil];
    
    //2.删除
    for (Person *p in result)
    {
        [context deleteObject:p];
    }
    
    //3.保存
    [context save:nil];
}


//更新数据
- (void)update
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = delegate.managedObjectContext;

    //1.先找到要更新的内容
    //  1)创建查询请求
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"]; // select * from Person
    //  2)设置查询条件 where name = "李四"
#warning --- NSPredicate的其他用途
    //      NSPredicate  谓词
    //      beginsWith   张%
    //      endsWith     %张
    //      contains     %张%
    //       >, <, >=, =
    //      and, or
    //      [c]     忽略大小写
    NSPredicate *where = [NSPredicate predicateWithFormat:@"name contains[c] %@", @"a"];
    request.predicate = where; // select * from Person where name = 李四
    
    //  3)执行查询,获取结果,数组中的每个元素都是Person类型的对象
    NSArray *result = [context executeFetchRequest:request error:nil];
    
    Person *p1 = result[0];
//    Person *p2 = result[1];
    //2.修改属性
    p1.name = @"王五";  p1.age = @(99);
    
    //3.保存修改
    [context save:nil];
}

//插入数据
- (void)insert
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    
    //相当于FMDatabase对象,用来处理数据的增删改查
    NSManagedObjectContext *context = delegate.managedObjectContext;
    
    //创建模型,Coredata模型不能直接实例化
    Person *p = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
    
    p.name = @"A张五三";
    p.age = @(18);//CoreData中的所有数据都是对象
    
    Person *p2 = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
    
    p2.name = @"a李五🐔";
    p2.age = @(28);//CoreData中的所有数据都是对象
    
    //上述代码相当于生成了一句sql语句 insert into Person(name, age) values(张三, 18)
    
    //可以同时保存多组数据
    [context save:nil]; //相当于fmdatabase中的excuteUpdate
}



- (void)predicate
{
    Cat *c1 = [[Cat alloc] init];
    Cat *c2 = [[Cat alloc] init];
    
    c1.name = @"小白";
    c1.age = 2;
    
    c2.name = @"小黑";
    c2.age = 3;
    
    //谓词在数组中的用法
    NSArray *arr = @[c1, c2];
    //通过谓词来查找数组中需要的内容, self == 当前元素
    //@"self.length = %ld", 2
    //@"self contains[c] %@", @"a"
    NSPredicate *p = [NSPredicate predicateWithFormat:@"self.name = %@", @"小白"];
    
    //利用谓词对数组进行过滤操作
    NSArray *result = [arr filteredArrayUsingPredicate:p];
    
    Cat *c = result[0];
    NSLog(@"result = %ld", c.age);
    
}


@end






